﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Threading;

namespace WebStudio.Mvc.Core.Filter
{

    /// <summary>
    ///  日志属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class LogMessageAttribute : FilterAttribute, IActionFilter, IResultFilter
    {

        //属性

        #region 日志文件路径
        /// <summary>
        /// <param name="LogName ">日志文件路径</para>
        /// </summary>
        public string LogName { get; set; }

        #endregion


        //方法

        #region 记录信息
        /// <summary>
        /// 记录时间，系统版本，当前线程ID 等记录
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="message"></param>
        public void LogMessage(string controller, string action, string message)
        {
            if (!string.IsNullOrEmpty(LogName))
            {
                TextWriter writer = new StreamWriter(LogName, true);
                writer.WriteLine("------------- Begin -------------");
                writer.WriteLine("Time:[{0}]", DateTime.Now.ToString("yyyy-MM-dd- hh:mm:ss"));
                writer.WriteLine("Controller:{0}", controller);
                writer.WriteLine("Action:{0}", action);
                writer.WriteLine("Message:{0}", message);
                writer.WriteLine("Operating System version is:{0}", System.Environment.OSVersion.Version.ToString());
                writer.WriteLine("Current Thread ID is:{0}", Thread.CurrentThread.ManagedThreadId);
                writer.WriteLine("------------- End -------------");
                writer.Close();
            }
        }

        #endregion

        #region IActionFilter 实现

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            LogMessage(filterContext.RouteData.Values["controller"].ToString(),
                filterContext.RouteData.Values["action"].ToString(),
                "Action exeuting...");
        }
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            LogMessage(filterContext.RouteData.Values["controller"].ToString(),
                filterContext.RouteData.Values["action"].ToString(),
                "Action executed.");
        }

        #endregion

        #region IResultFilter 实现

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            LogMessage(filterContext.RouteData.Values["controller"].ToString(),
                filterContext.RouteData.Values["action"].ToString(),
                "Result executing...");
        }
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
            LogMessage(filterContext.RouteData.Values["controller"].ToString(),
                  filterContext.RouteData.Values["action"].ToString(),
                  "Result executed");
        }

        #endregion
    }

     //[Logging(LogName = @"D:\Project\Project\MVCProject\sky.ExtendMVCFramework\sky.ExtendMVCFramework\Log.log")]
     //public ActionResult DesplayEmployee()
     //{
     //    ViewData["Message"] = "Our employees welcome you to our site!";
     //    List<Employee> employees = new List<Employee>
     //     {
     //         new Employee {
     //             FirstName="sky",
     //             LastName="yang",
     //             Email = "weflytotti@163.com",
     //             Department ="Development"
     //         }
     //     };
     //    return View(employees);
     //} 

}
